package marcis.jarina.web.controller;

import marcis.jarina.db.hibernate.entities.Archive;
import marcis.jarina.db.hibernate.entities.JavaClass;
import marcis.jarina.db.services.DefaultArchiveService;
import marcis.jarina.db.services.DefaultJavaClassService;
import marcis.jarina.web.gui.entity.WrappedList;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


@SuppressWarnings({"SpringJavaAutowiringInspection"})
@Controller
@RequestMapping(value = "/jarinaservlet")
public class EntitiesController {

    private static final Logger logger = Logger.getLogger(EntitiesController.class);

    @Autowired
    private DefaultArchiveService defaultArchiveService;

    @Autowired
    private DefaultJavaClassService classServiceDefault;


    @RequestMapping(value = "list/archive/{page}.html", method = RequestMethod.GET)
    public ModelAndView displayArchiveList(@PathVariable("page") int page) {
        page = normalizeArchivePage(page);
        List<Archive> list = defaultArchiveService.getPagedList(page * WrappedList.PER_PAGE, WrappedList.PER_PAGE);
        ModelAndView modelAndView = new ModelAndView("archiveList");
        modelAndView.addObject("list", WrappedList.getWrappedList(list, page));
        return modelAndView;
    }

    private int normalizeArchivePage(int page) {
        if (page < 0) {
            return 0;
        }
        long total = defaultArchiveService.getTotalCount();
        logger.warn("Total nr of archives: " + total);
        if (page * WrappedList.PER_PAGE > total) {
            return (int) total / WrappedList.PER_PAGE - 1;
        }
        return page;
    }

    private int normalizeClassPage(int page, int archiveId) {
        if (page < 0) {
            return 0;
        }
        long total = classServiceDefault.getTotalCountOfClasses(archiveId);
        logger.warn("Total nr of classes for that archive: " + total);
        if (page * WrappedList.PER_PAGE > total) {
            return (int) total / WrappedList.PER_PAGE - 1;
        }
        return page;
    }

    @RequestMapping(value = "list/javaclass/{archiveId}/{page}.html", method = RequestMethod.GET)
    public ModelAndView displayClassList(@PathVariable("archiveId") String archiveId, @PathVariable("page") int page) {
        Archive parentArchive = defaultArchiveService.loadById(Long.valueOf(archiveId));
        page = normalizeClassPage(page, Integer.valueOf(archiveId));
        if (parentArchive == null) {
            ModelAndView problemView = new ModelAndView("problem");
            problemView.addObject("pricina", "Nadrazeny archiv s id=" + archiveId + " nenalezen");
            return problemView;
        }
        List<JavaClass> list = classServiceDefault.pagedFilter("archive", parentArchive, page * WrappedList.PER_PAGE, WrappedList.PER_PAGE);
        ModelAndView modelAndView = new ModelAndView("javaclassList");
        modelAndView.addObject("list", WrappedList.getWrappedList(list, page));
        modelAndView.addObject("archiveId", archiveId);
        return modelAndView;
    }


    @RequestMapping(value = "detail/archive/{id}.html", method = RequestMethod.GET)
    public ModelAndView displayArchiveDetail(@PathVariable("id") Long id) {
        Archive archive = defaultArchiveService.loadById(id);
        if (archive == null) {
            ModelAndView problemView = new ModelAndView("problem");
            problemView.addObject("pricina", "Archiv s id=" + id + " nenalezen");
            return problemView;
        }
        ModelAndView modelAndView = new ModelAndView("archiveDetail");
        modelAndView.addObject("entity", archive);
        //TODO finish
        List<JavaClass> list = classServiceDefault.pagedFilter("archive", archive, 0, 100);
        if (archive.getFilePath() != null) {
            String[] parts = archive.getFilePath().split("\\\\");
            modelAndView.addObject("fileName", parts[parts.length - 1]);
        }
        modelAndView.addObject("list", WrappedList.getWrappedList(list, 0));
        return modelAndView;
    }

    @RequestMapping(value = "detail/javaclass/{id}.html", method = RequestMethod.GET)
    public ModelAndView displayClassDetail(@PathVariable("id") Long id) {
        JavaClass javaClass = classServiceDefault.loadById(id);
        ModelAndView modelAndView = new ModelAndView("javaclassDetail");
        modelAndView.addObject("entity", javaClass);
        return modelAndView;
    }


}
